//
//  ScrollViewCell.m
//  OCUI3
//
//  Created by login on 15-6-30.
//  Copyright (c) 2015年 login. All rights reserved.
//

#import "ScrollViewCell.h"

@interface ScrollViewCell ()<UIScrollViewDelegate>
{
    UIScrollView *_pictureScrollView;
    UIPageControl *_pageControl;
    NSArray *_arrayImages;
    NSMutableArray *_viewController;
    BOOL isFromStart;
    NSTimer *_timer;
}
@end

@implementation ScrollViewCell

- (void)addContentView
{
    _arrayImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"image1.jpg"],[UIImage imageNamed:@"image2.jpg"],[UIImage imageNamed:@"image3.jpg"],nil];
    
    CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width;
    
    _pictureScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, 220)];
    [_pictureScrollView setPagingEnabled:YES];
    _pictureScrollView.showsHorizontalScrollIndicator = NO;
    _pictureScrollView.showsVerticalScrollIndicator = NO;
    _pictureScrollView.delegate = self;
    _pictureScrollView.contentSize = CGSizeMake(screenWidth*_arrayImages.count, 0);
    [self addSubview:_pictureScrollView];
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(_pictureScrollView.frame)-20, screenWidth, 20)];
    _pageControl.backgroundColor = [UIColor clearColor];
    _pageControl.currentPage = 0;
    _pageControl.numberOfPages = _arrayImages.count;
    [_pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
    [self addSubview:_pageControl];
    
    _viewController = [[NSMutableArray alloc] init];
    
    for (NSInteger i = 0; i < [_arrayImages count]; i++) {
        [_viewController addObject:[NSNull null]];
    }

    
    _timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(scrollPages) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
    
    [self loadScrollViewPage:0];
    [self loadScrollViewPage:1];
    [self loadScrollViewPage:2];

}

-(void)loadScrollViewPage:(NSInteger)page
{
    if (page >= _arrayImages.count) {
        return;
    }
    
    UIViewController *imageViewController = [_viewController objectAtIndex:page];
    if ((NSNull *)imageViewController == [NSNull null])
    {
        imageViewController = [[UIViewController alloc] init];
        [_viewController replaceObjectAtIndex:page withObject:imageViewController];
    }
    
    if (imageViewController.view.superview == nil) {
        CGRect frame = _pictureScrollView.frame;
        frame.origin.x = CGRectGetWidth(frame) * page;
        frame.origin.y = 0;
        imageViewController.view.frame = frame;
        
        //[self addChildViewController:imageViewController];
        [_pictureScrollView addSubview:imageViewController.view];
        [imageViewController didMoveToParentViewController:self];
        
        [imageViewController.view setBackgroundColor:[UIColor colorWithPatternImage:(UIImage *)[_arrayImages objectAtIndex:page]]];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pageWidth = CGRectGetWidth(_pictureScrollView.frame);
    NSInteger page = floor((_pictureScrollView.contentOffset.x -pageWidth/2)/pageWidth) +1;
    _pageControl.currentPage = page;
    
    [self loadScrollViewPage:page-1];
    [self loadScrollViewPage:page];
    [self loadScrollViewPage:page+1];
}

- (IBAction)changePage:(id)sender
{
    NSInteger page = _pageControl.currentPage;
    
    [self loadScrollViewPage:page - 1];
    [self loadScrollViewPage:page];
    [self loadScrollViewPage:page + 1];
    
    CGRect bounds = _pictureScrollView.bounds;
    bounds.origin.x = CGRectGetWidth(bounds) * page;
    bounds.origin.y = 0;
    [_pictureScrollView scrollRectToVisible:bounds animated:YES];
}

-(void)scrollPages{
    ++_pageControl.currentPage;
    CGFloat pageWidth = CGRectGetWidth(_pictureScrollView.frame);
    if (isFromStart) {
        [_pictureScrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        _pageControl.currentPage = 0;
    }
    else
    {
        [_pictureScrollView setContentOffset:CGPointMake(pageWidth*_pageControl.currentPage, _pictureScrollView.bounds.origin.y)];
        
    }
    if (_pageControl.currentPage == _pageControl.numberOfPages - 1) {
        isFromStart = YES;
    }
    else
    {
        isFromStart = NO;
    }
}

- (CGFloat)getCellHeight;
{
    return 220;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
